﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Text;
using System;
using System.Xml;

namespace hxweditor
{
	public class SkillEffectEditor :BaseEditorWindow<SkillEffectEditor>
	{
        private static SkillEffectEditor insdance;

		private static string workIdKey = "SkillEditorWorkId";
		private static int workId = 0;

		private static GUILayoutOption[] layoutoption = new GUILayoutOption[0];

        private SkillEffectVo skillEffectVo;

		SkillEffectEditor()
		{
			this.titleContent = new GUIContent("技能效果编辑器");
		}

        void Awake()
        {
            insdance = this;
        }

		void OnEnable ()
		{

            skillEffectVo = null;
			workId = PlayerPrefs.GetInt (workIdKey);
		}

		void OnDisable ()
		{
			PlayerPrefs.SetInt (workIdKey, workId);
		}

		void OnGUI ()
		{
			workId = ShowInputInt ("当前ID:", workId);

            if (workId > 0)
            {
                if (skillEffectVo == null || skillEffectVo.SkillID != workId)
                {
                    ReadSkill();
                }
            }

			//写
			if (ShowButton ("保存目标ID技能")) {
				SaveSkill ();
			}

			//删除 
			if (ShowButton ("删除删除目标ID技能")) {
				RemoveSkill ();
				ReadSkill ();
			}

			//清理
			if (ShowButton ("清除技能所有属性")) 
            {
                if (skillEffectVo != null)
                    skillEffectVo.ActionList.Clear();
			}

			ShowSpaceLine (2);

			ShowLabelStatic ("属性:");

			if (ShowButton ("增加属性")) {
				SkillActionEditor.OpenWindow ();
			}

			ShowSpaceLine (3);

			ShowLabelStatic ("属性列表点击可编辑");

            if (skillEffectVo != null)
            {
                for (int i = 0; i < skillEffectVo.ActionList.Count; i++)
                {
                    SkillActionVo vo = skillEffectVo.ActionList[i];
                    if (ShowButton(vo.ToString(), layoutoption))
                    {
                        if (!SkillActionEditor.IsOpen)
                        {
                            SkillActionEditor.OpenWindow();
                        }
                        SkillActionEditor.SetActionVo(vo);
                    }
                }
            }

		}

		private void SaveSkill ()
		{
            if (skillEffectVo != null)
            {
                FileTool.WriteFile("E:/Winter/Winter/Assets/Resources/SkillData/" + skillEffectVo.SkillID + ".xml", skillEffectVo.ToXmlString());

                AssetDatabase.Refresh();    
            }
        }

		private void ReadSkill ()
		{
            try
            {
                TextAsset textAsset = (TextAsset)Resources.Load("SkillData/" + workId);
                skillEffectVo = SkillEffectVo.Create(textAsset.text);
                Debug.LogError(skillEffectVo.SkillID);
            }
            catch (Exception e)
            {
                //Debug.LogError("没有加载到Id=" + workId + e.ToString());
                skillEffectVo = new SkillEffectVo();
                skillEffectVo.SkillID = workId;
            }
		}

		public void RemoveSkill ()
		{
            if(workId > 0)
            {
                FileTool.RemoveFile("E:/Winter/Winter/Assets/Resources/SkillData/" + workId + ".xml");
                skillEffectVo = null;
                workId = 0;
                AssetDatabase.Refresh();
            }
		}

        public static void SetActionVo(SkillActionVo vo)
		{
            if (insdance != null)
            {
                insdance.AddActionVo(vo);
            }
            else
            {
                ShowAlert("窗口出错!");
            }
		}

        private void AddActionVo(SkillActionVo vo)
        {
            if(skillEffectVo == null && workId > 0)
            {
                skillEffectVo = SkillEffectVo.Create(workId);
            }

            skillEffectVo.AddAction(vo);

            AssetDatabase.Refresh();

            AssetDatabase.SaveAssets();

            FocusThis();
        }

		public static void RemoveActionVo (int propId)
		{
            if (insdance != null && insdance.skillEffectVo != null)
            {
                insdance.skillEffectVo.RemoveAction(propId);
            }
			FocusThis ();
		}

	}
}
